#include "cintelhexline.h"


CIntelHexLine::CIntelHexLine()
{
    this->type = 0xFF;
    this->addr = 0xFFFF;
    this->data_size = 0;
}

bool CIntelHexLine::SetLine( QString i_line )
{
    bool lo_success = true;

    this->type = 0xFF;
    this->addr = 0xFFFF;
    this->data_size = 0;
    memset(this->data,0xFF,DATA_MAX_SIZE);

    if( i_line.at(0) != ':' )
    {
        lo_success = false;
    }
    else
    {
        // verifie le checksum
        unsigned char calc_chk = 0;
        unsigned char chk;
        QString tmp_str;
        unsigned char loop;

        // enleve le ':'
        i_line.remove(0,1);

        for( loop=0; loop<(i_line.size()-2); loop+=2 )
        {
            tmp_str = i_line.at(loop);
            tmp_str.append(i_line.at(loop+1));
            calc_chk += tmp_str.toUInt(NULL, 16);
        }

        tmp_str = i_line.at(i_line.size()-2);
        tmp_str.append( i_line.at(i_line.size()-1) );
        chk = tmp_str.toUInt(NULL, 16);

        calc_chk = ~calc_chk;
        calc_chk += 1;

        if( chk != calc_chk )
        {
            lo_success = false;
        }
        else
        {
            // on va analyser la ligne
            tmp_str = i_line.at(0);
            tmp_str.append( i_line.at(1) );
            this->data_size = tmp_str.toUInt(NULL, 16);
            if( this->data_size >= DATA_MAX_SIZE )
            {
                lo_success = false;
            }
            else
            {
                tmp_str = i_line.at(2);
                tmp_str.append( i_line.at(3) );
                tmp_str.append( i_line.at(4) );
                tmp_str.append( i_line.at(5) );
                this->addr = tmp_str.toUInt(NULL, 16);
                tmp_str = i_line.at(6);
                tmp_str.append( i_line.at(7) );
                this->type = tmp_str.toUInt(NULL, 16);
                for( loop=0; loop<this->data_size; loop++ )
                {
                    tmp_str = i_line.at(8+(2*loop));
                    tmp_str.append( i_line.at(9+(2*loop)) );
                    this->data[loop] = tmp_str.toUInt(NULL, 16);
                }
            }
        }
    }

    return lo_success;
}

unsigned char CIntelHexLine::getType( void )
{
    return this->type;
}

unsigned short CIntelHexLine::getAddr( void )
{
    return this->addr;
}

unsigned char CIntelHexLine::getDataSize( void )
{
    return this->data_size;
}

unsigned char *CIntelHexLine::getData( void )
{
    return this->data;
}

